\documentclass[a4paper, fullpage]{article}

\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
%\usepackage[swedish]{babel}
\usepackage[intlimits]{amsmath}
\usepackage{amssymb}
\usepackage{verbatim}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{float}
\usepackage[all]{xy}

%\graphicspath{{./pix/}}

\addtolength{\voffset}{-35pt}
\addtolength{\textheight}{75pt}


\newcommand{\unit}[1]{\ensuremath{\, \mathrm{#1}}}

\setlength{\parindent}{0in}

\newcommand{\vect}[1]{\boldsymbol{#1}}
%\newcommand{\vect}[1]{\vec{#1}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\title{Autonomous agents (FFR125)\\ Robtics project 1}
\author{
Viktor Jonsson, v.a.jonsson@gmail.com\\
Per Ringqvist, perringqvist@gmail.com\\
Olov Mattsson, olov.ma@gmail.com\\
Sebastian Johansson, johansson.seb@gmail.com}
\date{\today}

\maketitle
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{The Thought behind the design}
The plan when building the front sensors was to make it impossible
for the robot to rotate with its back wheel onto a mine. This was
achieved by placing the front sensors so far away from the center
of the robot that area checked by the sensors was large enough to
allow a full rotation. That is the minimum radius that a mine would
be able to pass undetected by the robot would still be larger than
the radius to the back wheel. And even if a mine has been detected
a 360 degree rotation would still be possible without touching the
mine. The choice to use 5 reflex sensors was made to prevent any mine
to pass too far under the front fork undetected.

\begin{figure}[H]
 \begin{center}
 \includegraphics[width=0.75\textwidth]{boe.png}
  \caption{\label{d1}Our Boebot. Note the five reflex sensors on the front fork. The two ir-detector pairs are situated in front the white circuit board.}
 \end{center}
\end{figure}

The reflex sensors consists of an IR diode and a photoresistor
placed close together. The photoresistors give analog output but
we used the basic circuit seen in fig 2 to convert the signal to
digital output. This was done by building a circuit board containing
5 of the circuits shown in fig~\ref{d2}. The circuits were slightly
modified and the diodes were connected to one pin each while the
photo resistors were all connected to the same pin. This means that
to read the five sensors we in turn turned the first diode on, read
the output pin and turned the diode off. This was then repeated for
all the five reflex sensors.

\begin{figure}[H]
 \begin{center}
 \includegraphics[width=0.75\textwidth]{qti.pdf}
  \caption{\label{d2}Circuit for converting the analog photoresistor
signal to digital. Schema from the manual for the QTI Line Follow kit
for the BoeBot \cite{qti}. We replaced $470 \Omega$ resistor with a $120
\Omega$ eqvivalent. This was an adaptation to the arena floor-conditions.}
 \end{center}
\end{figure}

The collision avoidence was handled by 2 IR diode and detector pairs
facing forward as seen in fig~\ref{d1}.

\section{The brain}
The brain has three states with corresponding utilities $u_1$, $u_2$ and $u_3$:
\begin{enumerate}
\item{\emph{Collision avoidance}}
\item{\emph{Mine detect}}
\item{\emph{Move forward}}
\end{enumerate}

The brain contains 2 state variables, $s_1$ and $s_2$. The value of
$s_1$ is determined by the IR-sensors that detects walls. We set
$s_1=1$ if a wall is detected, that is, whenever one of the 2 IR
sensors gives a reading, and $s_1=0$ otherwise. The value of $s_2$ is
determined by the photoresitors. If one or more of the photoresistors
detects a mine, we set $s_2=1$, or else $s_2=0$. Note that the state
variables and utilities are only updated during the move forward
state. In the other states no motion except for pure rotations are
allowed, why it should be impossible to hit a mine or a wall. The
utilities are set as follows:

\begin{enumerate}
\item{$u_1 = 3s_1$}
\item{$u_2 = 2s_2$}
\item{$u_3 = 1$ (constant)}
\end{enumerate}

The purpose of this is simply that \emph{Move forward} should be
active if no sensor readings indicates a mine or a wall. Whenever a
wall is detected, the brain should move into state \emph{Collision
avoidance}, and whenever a mine is detected \emph{mine detect} is
activated. In the event of a wall being detected at the same time as
a mine, \emph{Collision avoidance} is activated since the utilities
will then be $u_1=3$ and $u_2=2$. The priorization of \emph{Collision
avoidance} over \emph{Mine detect} is of minor importance was set
quite arbitrarly. The utilities are not explicitly used in the actual
code, because it was not necessary to assign actual utility values
to choose between the states. In the program this is just implemented
by checking if $s_1=1$ before checking $s_2=1$. A description of the
3 states is given in the subsections below.

\subsection{\emph{Collision avoidance}}
This state is activated whenever one of the IR sensors gives a
reading, indicating a wall nearby. If the right ir-sensor reads,
the robot will turn a little less than 90 degrees to the left, and
vice vesa for the left IR sensor.

\subsection{\emph{Mine detect}}
This state is activated when one of the photo resistors detects
something. The first thing that happens is that a brief pulse is
sent to the servos in order to shift the robot slightly from its
position. It then stands still and takes a second reading of all its
photo resistor sensors. If one of them is still active it moves on
to activate the signal LED and then rotate a given angle depending on
which sensor was activated. If the rightmost sensor is active it turns
a small angle to the left and vice versa for the leftmost sensor. The
second rightmost sensor turns by a larger angle to the left andvice
versa for the left one. If the middle sensor is activated it turns
approximately 90 degrees and in this case it turns to the left every
second time the middle sensor is activated and to the right the
other times.  \\ \\ The idea of the small shift of the robot and
reading the sensors again is to give the robot a second chance to
avoid false positives, i.e. detecting mines where there are no mines.

\subsection{\emph{Move forward}}
In this state the robot moves forward in a straight line, all the
time reading the sensors and updating the state variables. Note that
every time this state is entered, the state variables are updated
again before any forward motion.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\raggedright
\begin{thebibliography}{9}

\bibitem{qti}
Parallax Inc (April 2010)
\emph{QTI Line Follower AppKit for the Boe-Bot.}
http://www.parallax.com/dl/docs/prod/robo/QTILineFollow.pdf

\end{thebibliography}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\appendix
\section{Code}
\begin{verbatim}
' -----[ Title ]--------------------------------------------------------------
' {$STAMP BS2}                               ' Stamp directive.
' {$PBASIC 2.5}                              ' PBASIC directive.
DEBUG "Program Running!"
' -----[ Variables ]----------------------------------------------------------

il VAR BIT
ir VAR BIT
r1 VAR BIT
r2 VAR BIT
r3 VAR BIT
r4 VAR BIT
r5 VAR BIT
s1 VAR BIT
s2 VAR BIT
x  VAR BIT
c VAR BYTE
x=1
' -----[ Main Routine ]-------------------------------------------------------
DO

GOSUB Irsensors
GOSUB ReflexSensor
GOSUB Utility

LOOP	
' -----[ Subroutine-Irsensors ]--------------------------------------------------------

Irsensors:

FREQOUT 8, 1, 42500
il = IN9

FREQOUT 2, 1, 42500
ir = IN0

IF (il=0) OR (ir=0) THEN
s1=1
ELSE
s1=0
ENDIF

RETURN
' -----[ Subroutine-ReflexSensor ]--------------------------------------------------------
ReflexSensor:

HIGH 6: PAUSE 1:  r1   =IN7: INPUT 6
HIGH 10: PAUSE 1: r2   =IN7: INPUT 10
HIGH 11: PAUSE 1: r3 =IN7: INPUT 11
HIGH 14: PAUSE 1: r4  =IN7: INPUT 14
HIGH 15: PAUSE 1: r5  =IN7: INPUT 15


IF (r1 = 1) OR (r3=1) OR (r5=1) OR (r2=1) OR (r4=1) THEN 
s2=1
ELSE
s2=0
ENDIF

RETURN

' -----[ U=Utility ]--------------------------------------------------------
Utility:

IF (s1=1) THEN 
GOSUB Navigate
ELSEIF (s2=1) THEN
GOSUB MineDetect
ELSE
GOSUB MoveForward
ENDIF
RETURN

' -----[ Navigate ]--------------------------------------------------------

Navigate:

IF (il=0) AND (ir=0) THEN 
	GOSUB RotateOne


ELSEIF (il=0) THEN 
	GOSUB RotateTwo


ELSEIF (ir=0) THEN 
	GOSUB RotateThree

ENDIF

RETURN 

' -----[ RotateOne ]--------------------------------------------------------

RotateOne:

FOR c= 1 TO 48

PULSOUT 13, 850
PULSOUT 12, 850 

PAUSE 20
NEXT 


RETURN

' -----[ RotateTwo ]--------------------------------------------------------

RotateTwo:

FOR c= 1 TO 18

PULSOUT 13, 850
PULSOUT 12, 850 

PAUSE 20
NEXT 


RETURN

' -----[ RotateThree ]--------------------------------------------------------

RotateThree:

FOR c= 1 TO 18

PULSOUT 13, 650
PULSOUT 12, 650 
PAUSE 20

NEXT 

RETURN

' -----[ Rotatefour ]--------------------------------------------------------

RotateFour:

FOR c= 1 TO 8

PULSOUT 13, 650
PULSOUT 12, 650 
PAUSE 20

NEXT

RETURN  
' -----[ Rotatefive ]--------------------------------------------------------

RotateFive:

FOR c= 1 TO 8

PULSOUT 13, 850
PULSOUT 12, 850 
PAUSE 20

NEXT 

RETURN


' -----[Subrotine MineDetect]--------------------------------------------------------
MineDetect:

IF (r1=1) OR  (r2=1) OR (r3=1) THEN
FOR c= 1 TO 3

PULSOUT 13, 740
PULSOUT 12, 740 
PAUSE 20
NEXT 

ELSE 
FOR c= 1 TO 3
PULSOUT 13, 760
PULSOUT 12, 760 
PAUSE 20
NEXT 

ENDIF 

PAUSE 500

HIGH 6: PAUSE 1:  r1   =IN7: INPUT 6
HIGH 10: PAUSE 1: r2   =IN7: INPUT 10
HIGH 11: PAUSE 1: r3 =IN7: INPUT 11
HIGH 14: PAUSE 1: r4  =IN7: INPUT 14
HIGH 15: PAUSE 1: r5  =IN7: INPUT 15

IF (r1=1) THEN 

GOSUB Light 
GOSUB RotateFive


ELSEIF (r2=1) THEN
 
GOSUB Light
GOSUB RotateTwo

ELSEIF (r3=1) THEN
IF (x=1) THEN

GOSUB Light
GOSUB RotateTwo
x = 0
ELSE

GOSUB Light
GOSUB RotateThree
x = 1
ENDIF

ELSEIF  (r4=1) THEN 

GOSUB Light
GOSUB RotateThree

ELSEIF (r5=1) THEN
GOSUB Light
GOSUB Rotatefour 

ENDIF

RETURN

' -----[Subrotine MoveForward]--------------------------------------------------------
MoveForward:

PULSOUT 13, 850
PULSOUT 12, 650
PAUSE 1
RETURN

' -----[Subrotine Light]--------------------------------------------------------
Light:

FOR c= 1 TO 1

HIGH 4
PAUSE 250
LOW 4
PAUSE 250
NEXT 


RETURN
\end{verbatim}

\end{document}

